//
//  LJJMyTableView.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/14.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJMyTableView.h"
#import "LJJMyCell.h"

static NSString *identifier = @"myCell";

@interface LJJMyTableView () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSArray *iconArray;    /** icon名 **/

@end

@implementation LJJMyTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
//        self.bounces = NO;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = MAINCOLOR;
        [self registerClass:[LJJMyCell class] forCellReuseIdentifier:identifier];
        self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    }
    return self;
}
- (void)exitMethod:(UIButton *)sender
{
    self.exitBlock();
}
#pragma mark -
#pragma mark - tableView delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"]) {
        return 6;
    }
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJJMyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.dic = self.iconArray[indexPath.row];
    if (indexPath.row == 3) {
        UIImageView *imageView = [cell valueForKey:@"nextImageView"];
        imageView.hidden = YES;
        UILabel *phoneLabel = [[UILabel alloc] init];
        phoneLabel.text = @"400-517-517";
        phoneLabel.textColor = RGB(123, 124, 128, 1);
        [cell addSubview:phoneLabel];
        //约束
        __weak typeof(cell) weakSelf = cell;
        [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.mas_right).offset(-15);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(110, 15));
        }];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LJJLog(@"选中第%ld行", (long)indexPath.row);
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"];
    if (dic.count) {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
        footView.backgroundColor = MAINCOLOR;
        UIButton *exitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        exitBtn.frame = CGRectMake(50, 42, WIDTH - 100, 45);
        [exitBtn addTarget:self action:@selector(exitMethod:) forControlEvents:(UIControlEventTouchUpInside)];
        [exitBtn setImage:[UIImage imageNamed:@"我的界面退出登录按钮"] forState:(UIControlStateNormal)];
        [footView addSubview:exitBtn];
        return footView;
    }
    return nil;
}
#pragma mark -
#pragma mark - scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 0) {
        self.bounces = YES;
        NSLog(@"%f, height = %f", scrollView.contentOffset.y, self.frame.size.height);
    }
    else
    {
        self.bounces = NO;
    }
}
#pragma mark -
#pragma mark - lazy method
- (NSArray *)iconArray
{
    if (!_iconArray) {
        _iconArray = @[
                       @{@"name" : @"我的收藏",
                         @"image" : @"我的界面我的收藏图标"},
                       @{@"name" : @"意见反馈",
                         @"image" : @"我的界面意见反馈图标"},
                       @{@"name" : @"关于我们",
                         @"image" : @"我的界面关于我们图标"},
                       @{@"name" : @"客服热线",
                         @"image" : @"我的界面客服热线图标"},
                       @{@"name" : @"我的优惠券",
                         @"image" : @"我的界面我的优惠券图标"},
                       @{@"name" : @"邀请好友，立刻赚钱",
                         @"image" : @"我的界面邀请好友图标"}
                       ];
    }
    return _iconArray;
}

@end
